package com.ruoyi.web.tool;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一返回结果类
 *
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    private Integer code;
    private String msg;

    private T data;

    public Result(Integer code, String message) {
        this.code = code;
        this.msg = message;
    }


    public Result(ResultCode resultCode) {
        this.code = resultCode.value();
        this.msg = resultCode.description();
    }

    public Result(ResultCode resultCode, T data) {
        this(resultCode);
        this.data = data;
    }


    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS, data);
    }


    public static <T> Result<T> success(ResultCode resultCode) {
        return new Result<>(resultCode.value(), resultCode.description());
    }

    public static <T> Result<T> success(T data, String message) {
        return new Result<>(ResultCode.SUCCESS.value(), message, data);
    }


    public static <T> Result<T> failed() {
        return new Result<T>(ResultCode.FAILED.value(), ResultCode.FAILED.description());
    }

    public static <T> Result<T> failed(ResultCode resultCode) {
        return new Result<>(resultCode.value(), resultCode.description());
    }

    public static <T> Result<T> failed(Integer code, String message) {
        return new Result<>(code, message);
    }

    public static <T> Result<T> failed(String message) {
        return new Result<>(ResultCode.FAILED.value(), message);
    }

    public Result custom(ResultCode resultCode) {
        this.code = resultCode.value();
        this.msg = resultCode.description();
        return this;
    }


}
